// pages/money/money.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentMoney: "0.00",
    specialChar: [],
    rest: "",
    remark: "",
    dataString: [
      [
        { lable: "1", value: "1", variant: "outline" },
        { lable: "2", value: "2", variant: "outline" },
        { lable: "3", value: "3", variant: "outline" },
        { lable: "取消", value: "取消" },
      ],
      [
        { lable: "4", value: "4", variant: "outline" },
        { lable: "5", value: "5", variant: "outline" },
        { lable: "6", value: "6", variant: "outline" },
        { lable: "清空", value: "清空" },

      ], [
        { lable: "7", value: "7", variant: "outline" },
        { lable: "8", value: "8", variant: "outline" },
        { lable: "9", value: "9", variant: "outline" },
        { lable: "+", value: "+", variant: "outline" },

      ],
      [
        { lable: "0", value: "0", variant: "outline" },
        { lable: ".", value: ".", variant: "outline" },
        { lable: "-", value: "-", variant: "outline" },
        { lable: "确定", value: "确定", theme: "primary" }
      ],
    ],
    priceFormat: (v) => {
      const isNumber = /^\d+(\.\d+)?$/.test(v);
      if (isNumber) {
        return parseFloat(v).toFixed(2);
      }
      return v;
    },
    priceError: false,
  },
  applyOperator(operand1, operand2, operator) {
    switch (operator) {
      case "+":
        return operand1 + operand2;
      case "-":
        return operand1 - operand2;
      case "*":
        return operand1 * operand2;
      case "/":
        return operand1 / operand2;
    }
  },
  evalMath(expression) {
    // 将表达式转换为标记数组
    const tokens = expression.match(/[+\-*/()]|[-+]?(\d+(\.\d*)?|\.\d+)/g);

    // 定义操作数和操作符的堆栈
    const operands = [];
    const operators = [];

    // 定义优先级映射
    const precedence = {
      "+": 1,
      "-": 1,
      "*": 2,
      "/": 2,
    };

    // 处理每个标记
    for (const token of tokens) {
      if (/^[\d\.]+$/.test(token)) {
        // 如果标记是一个数字，将其作为操作数压入堆栈
        operands.push(Number(token));
      } else if (/^[+\-*/]$/.test(token)) {
        // 如果标记是一个操作符，将其与堆栈中的操作符进行比较，
        // 并根据优先级将操作符弹出并应用于操作数
        while (
          operators.length > 0 &&
          precedence[operators[operators.length - 1]] >= precedence[token]
        ) {
          const operator = operators.pop();
          const operand2 = operands.pop();
          const operand1 = operands.pop();
          operands.push(this.applyOperator(operand1, operand2, operator));
        }
        operators.push(token);
      } else if (token === "(") {
        // 如果标记是左括号，将其压入操作符堆栈
        operators.push(token);
      } else if (token === ")") {
        // 如果标记是右括号，将堆栈中的操作符弹出并应用于操作数，直到找到左括号为止
        while (operators[operators.length - 1] !== "(") {
          const operator = operators.pop();
          const operand2 = operands.pop();
          const operand1 = operands.pop();
          operands.push(this.applyOperator(operand1, operand2, operator));
        }
        operators.pop(); // 弹出左括号
      }
    }

    // 处理堆栈中剩余的操作符
    while (operators.length > 0) {
      const operator = operators.pop();
      const operand2 = operands.pop();
      const operand1 = operands.pop();
      operands.push(this.applyOperator(operand1, operand2, operator));
    }

    // 返回最终结果
    return operands[0];
  },
  onTabsChange(event) {
    console.log(`Change tab, tab-panel value is ${event.detail.value}.`);
  },

  onTabsClick(event) {
    console.log(`Click tab, tab-panel value is ${event.detail.value}.`);
  },


  onPriceInput(e) {
    const { priceError } = this.data;
    const isNumber = /^\d+(\.\d+)?$/.test(e.detail.value);
    if (priceError === isNumber) {
      this.setData({
        priceError: !isNumber,
      });
    }
  },
  handleSpecialClick(type) {
    this.setData({
      specialChar: [...this.data.specialChar, type]
    })
  },
  changeMoney(e) {
    const value = e.currentTarget.dataset.value;
    console.log('value', value);
    if (value == "清空") {
      this.setData({
        currentMoney: "0.00"
      })
    }
    else if (value == "取消") {
      if (this.data.currentMoney == 0) {
        return
      }
      console.log('点击取消');
      this.setData({
        currentMoney: this.data.currentMoney.substring(0, this.data.currentMoney.length - 1)
      })
    }
    else if (value == "⇤") {
      if (this.data.currentMoney == 0) {
        return
      }
      this.setData({
        currentMoney: this.data.currentMoney.substring(0, this.data.currentMoney.length - 1)
      })
    }
    else if (value == "确定") {
      this.handleMoneySubmit()
    }
    else {
      console.log("else");
      if (this.data.currentMoney == 0) {
        this.setData({
          currentMoney: value
        })
      }else {
        this.setData({
          currentMoney: this.data.currentMoney + value
        })
      }
    }
  },
  inputChange(e) {
    this.setData({
      remark: e.detail.value
    });
  },
  handleMoneySubmit() {
    const money = this.evalMath(this.data.currentMoney);
    const data = {
      remark: this.data.remark,
      money: money
    }
    wx.cloud.callFunction({
      name: 'BillRecordCreate',
      data,
      complete: res => {
        console.log("res", res);
      },
    })
    // this.setData({
    //   currentMoney: money
    // })
    this.setData({
      currentMoney: "0.00"
    })
    this.setData({
      remark: ""
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(this.data.dataString);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})